<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分区名称" prop="areaName">
        <el-input
            v-model="queryParams.areaName"
            placeholder="请输入分区名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['water:area:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['water:area:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['water:area:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['water:area:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="areaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="父区域id" align="center" prop="parentAreaId" />
      <el-table-column label="分区名称" align="center" prop="areaName" />
      <el-table-column label="分区层级" align="center" prop="layerNum" />
      <el-table-column label="预估用水量" align="center" prop="waterValue" />
      <el-table-column label="一级告警" align="center" prop="firstWarn" />
      <el-table-column label="二级告警" align="center" prop="secondWarn" />
      <el-table-column label="三级告警" align="center" prop="thirdWarn" />
      <el-table-column label="分区状态" align="center" prop="areaStatus" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['water:area:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 引入区域编辑组件 -->
    <AreaEdit
        :init-form="form"
        :open="open"
        :title="title"
        @submit="handleSubmit"
        @close="cancel"
        @update:open="(val) => open = val"
    />
  </div>
</template>

<script setup name="Area">
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/water/area"
import { getCurrentInstance, ref, reactive, toRefs } from 'vue'
import AreaEdit from '@/components/water/dialog/AreaEdit.vue'

const { proxy } = getCurrentInstance()

// 响应式数据
const areaList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const form = ref({})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    areaName: null,
  }
})

const { queryParams } = toRefs(data)

/** 查询区域管理列表 */
function getList() {
  loading.value = true
  listArea(queryParams.value).then(response => {
    areaList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  form.value = {}
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  form.value = {}
  open.value = true
  title.value = "添加区域管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row?.id || ids.value[0]
  getArea(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改区域管理"
  })
}

/** 处理表单提交 */
function handleSubmit(formData) {
  if (formData.id != null) {
    updateArea(formData).then(response => {
      proxy.$modal.msgSuccess("修改成功")
      open.value = false
      getList()
    })
  } else {
    addArea(formData).then(response => {
      proxy.$modal.msgSuccess("新增成功")
      open.value = false
      getList()
    })
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row?.id || ids.value
  proxy.$modal.confirm('是否确认删除区域管理编号为"' + _ids + '"的数据项？').then(function() {
    return delArea(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('water/area/export', {
    ...queryParams.value
  }, `area_${new Date().getTime()}.xlsx`)
}

// 初始化加载列表
getList()
</script>